/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.desmond.squarecamera.util;

import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.PixelMapHolder;
import ohos.agp.render.Texture;
import ohos.agp.utils.Matrix;
import ohos.agp.utils.RectFloat;
import ohos.media.image.ImagePacker;
import ohos.media.image.PixelMap;
import ohos.media.image.common.AlphaType;
import ohos.media.image.common.PixelFormat;
import ohos.media.image.common.ScaleMode;
import ohos.media.image.common.Size;
import java.io.OutputStream;

public class BitmapUtil {

    public static boolean compress(PixelMap pixelMap,
                                   String compressFormat,
                                   int quality,
                                   OutputStream outputStream) {
        if (pixelMap == null) {
            return false;
        }
        ImagePacker imagePacker = ImagePacker.create();
        ImagePacker.PackingOptions packingOptions = new ImagePacker.PackingOptions();
        packingOptions.format = compressFormat;
        packingOptions.quality = quality;
        boolean result = imagePacker.initializePacking(outputStream, packingOptions);
        if (result) {
            imagePacker.addImage(pixelMap);
            imagePacker.finalizePacking();
            return true;
        } else {
            return false;
        }
    }

    public static PixelMap createPixelMap(PixelMap source, int x, int y, int width, int height,
                                          Matrix matrix, boolean filter) {

        PixelMap.InitializationOptions initializationOptions = new PixelMap.InitializationOptions();

        final RectFloat srcR = new RectFloat(x, y, x + width, y + height);
        final RectFloat dstR = new RectFloat(0, 0, width, height);
        final RectFloat deviceR = new RectFloat();
        Paint paint = new Paint();
        paint.setFilterBitmap(filter);
        if (matrix != null) {
            final boolean transformed = !matrix.rectStaysRect();
            if (transformed) {
                paint.setAntiAlias(true);
            }
            matrix.mapRect(deviceR, dstR);
            int neww = Math.round(deviceR.getWidth());
            int newh = Math.round(deviceR.getHeight());
            initializationOptions.size = new Size(neww, newh);
        } else {
            initializationOptions.size = new Size((int) dstR.getWidth(), (int) dstR.getHeight());
        }

        initializationOptions.scaleMode = ScaleMode.CENTER_CROP;
        initializationOptions.alphaType = AlphaType.OPAQUE;
        PixelMap srcPixelMap = PixelMap.create(initializationOptions);
        Texture texture = new Texture(srcPixelMap);
        Canvas canvas = new Canvas(texture);

        canvas.translate(-deviceR.left, -deviceR.top);
        if (matrix != null) {
            canvas.concat(matrix);
        }
        canvas.drawPixelMapHolderRect(new PixelMapHolder(source), srcR, dstR, paint);
        return texture.getPixelMap();
    }

    public static PixelMap createPixelMap(int width, int height, PixelFormat pixelFormat) {
        PixelMap.InitializationOptions initializationOptions = new PixelMap.InitializationOptions();
        initializationOptions.size = new Size(width, height);
        initializationOptions.pixelFormat = pixelFormat;
        initializationOptions.editable = true;
        return PixelMap.create(initializationOptions);
    }
}
